package org.hackystat.ui.telemetryviewer.gwt.server;

import org.hackystat.ui.telemetryviewer.gwt.client.ChartServiceRPC;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Implements server side of the ChartGenerator service.
 *
 * @author Pavel Senin.
 *
 */
public class ChartServiceRPCImpl extends RemoteServiceServlet implements ChartServiceRPC {

  static final String URLDelimeter = "/";

  static final String defaultProject = "jfcs/file";

  private static final long serialVersionUID = -8261231970407498849L;

  /**
   * Generates chart according to parameters given.
   *
   * @param query specifies the chart type.
   * @return the path to the chart generated.
   */
  public String generateChart(String query) {
    /*
     * Hold our stored chart name, it will be returned to the GWT caller.
     */
    StringBuilder requestUrl = new StringBuilder(200);

    if ("dependency".equalsIgnoreCase(query)) {
      requestUrl.append(defaultProject);
      requestUrl.append(URLDelimeter);
      requestUrl.append("jfreechart-12435.png");
    }
    else if ("languages".equalsIgnoreCase(query)) {
      requestUrl.append(defaultProject);
      requestUrl.append(URLDelimeter);
      requestUrl.append("jfreechart-12434.png");
    }

    // Prepare the request
    // Request request = new Request(Method.GET, "http://localhost:3128/" + requestUrl);
    // request.setReferrerRef("http://www.mysite.org");
    //
    // // Handle it using an HTTP client connector
    // Client client = new Client(Protocol.HTTP);
    // Response response = client.handle(request);
    //
    // // Write the response entity on the console
    // Representation output = response.getEntity();

    return requestUrl.toString();
  }

}